package org.loofer.ext.component.thread;

import android.annotation.TargetApi;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

public final class ThreadPools {

    private static class DefaultHolder {
        public static final PriorityThreadPool INSTANCE = new PriorityThreadPool();

        private DefaultHolder() {
        }
    }

    private ThreadPools() {
    }

    public static PriorityThreadPool defaultThreadPool() {
        return DefaultHolder.INSTANCE;
    }

    public static ThreadPool newCachedThreadPool(String name) {
        return newCachedThreadPool(name, 60, TimeUnit.SECONDS);
    }

    public static ThreadPool newCachedThreadPool(String name, long timeout, TimeUnit timeunit) {
        return new ThreadPool(name, 0, Integer.MAX_VALUE, timeout, timeunit, new SynchronousQueue());
    }

    @TargetApi(9)
    public static ThreadPool newCachedThreadPool(String name, int maxThreads) {
        return newCachedThreadPool(name, maxThreads, 60, TimeUnit.SECONDS);
    }

    @TargetApi(9)
    public static ThreadPool newCachedThreadPool(String name, int maxThreads, long timeout, TimeUnit timeunit) {
        return new ThreadPool(name, maxThreads, timeout, timeunit);
    }

    public static ThreadPool newFixedThreadPool(String name, int nThreads) {
        return new ThreadPool(name, nThreads, nThreads, 0, TimeUnit.MILLISECONDS, new LinkedBlockingQueue());
    }
}
